package com.yatuhashi.base.db;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {
    private static DataBaseHelper dbHelper;
    private static final String DB_NAME = "sample.db";
    private static final int DB_VERSION = 1;
    private static Context c;
    
    public DataBaseHelper(Context c) {
        super(c, DB_NAME, null, DB_VERSION);
        this.c = c;
    }
    
    public static DataBaseHelper getInstance(Context c) {
        if(dbHelper==null) {
            dbHelper = new DataBaseHelper(c);
        }
        return dbHelper;
    }
    
    public DataBaseHelper(Context c, String name, CursorFactory factory, int ver) {
        super(c, name, factory, ver);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        StringBuffer sql = new StringBuffer();
        sql.append("create table MyTable (");
        sql.append(" No integer primary key autoincrement");
        sql.append(",Name text not null");
        sql.append(",Tel text");
        sql.append(",Age integer");
        db.execSQL(sql.toString());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
    
    public static void createDb(Context c) {
        if(!dbExist()) {
            getInstance(c);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            db.close();
            
            String dbFile = c.getDatabasePath(DB_NAME).getPath();
        }
    }
    
    /**
     * DataBaseが以前作成されているかどうか
     * @return boolean
     */
    private static boolean dbExist() {
        SharedPreferences pref = c.getSharedPreferences(DataBaseConsts.DB_LOCAL, Context.MODE_PRIVATE);
        return pref.getBoolean(DataBaseConsts.DB_CREATED, false);
    }
}
